package  
{
  import away3d.containers.View3D;
  import away3d.materials.BitmapMaterial;
  import flash.display.Bitmap;
  import flash.geom.Rectangle;
  import flash.geom.Vector3D;
  
	/**
   * TODO This class should extend Mesh
   * @author Ivan
   */
  public class ComplexHeightMap 
  {
    private const TEXTURE_SMOOTH:Boolean = true;
    private const TEXTURE_MIPMAP:Boolean = true;
        
    [Embed(source="../embeds/height_map.png")]
		private var rHeightMap : Class;
    
    private var _view:View3D;
    private var _camController:HoverDragController;
        
    public function ComplexHeightMap(_view:View3D, _camController:HoverDragController = null) 
    {
      this._camController = _camController;
      this._view = _view;
      buildHeightMap(4, false);
    }
    
    // TODO Reenable async loading
    private function buildHeightMap(plots:int, async:Boolean):void
    { 
        
      /*
        function iterate(e:Event):void
        {
          iterator_count++;
          if (iterator_count < 0) return;
          iterator_count = 0;
          if (heightMapIteration(plots))
            removeEventListener(Event.ENTER_FRAME, iterate);
        }*/
        
        //if (async)
//          addEventListener(Event.ENTER_FRAE, iterate);
        //else
        {
          var completed:Boolean;
          while (!completed)
            completed = heightMapIteration(plots);
        }
    }
    private var iterator_count:int;
    
    private var iteration_i:int;
    private var iteration_j:int;
    public var heightBmp:Bitmap = new rHeightMap();
    private function heightMapIteration(plots:int):Boolean
    {
      var bmp_w:int = heightBmp.width / plots;
      var bmp_h:int = heightBmp.height / plots;
      
      var region:Rectangle = new Rectangle(iteration_i * bmp_w, iteration_j * bmp_h, bmp_w, bmp_h)
      var hmt:HeightMapTexture = new HeightMapTexture(heightBmp.bitmapData, region);
      //var material:BitmapMaterial = new BitmapMaterial((new rSand() as Bitmap).bitmapData, true, true); // smooth, repeated
      var material:BitmapMaterial = new BitmapMaterial(hmt.bitmapData, TEXTURE_SMOOTH, false, TEXTURE_MIPMAP); // smooth, repeated
      //material.ambientColor = 0xFFAAAA;
      //material.specular = 0.1;
      //material.lights = [_light];
      
      var hm:HeightMap = new HeightMap( heightBmp.bitmapData, 
                                        region, 
                                        0, 0, 
                                        material);
      
      hm.x = iteration_i * hm.width;
      hm.z = iteration_j * hm.width;
      /*
      var hm:Elevation = new Elevation( material, heightBmp.bitmapData, 1500, 100, 1500, 64, 64);
      hm.x = iteration_i * hm.width;
      hm.z = (plots - iteration_j -1) * hm.width;
      */
      
      _view.scene.addChild(hm);
      heightMaps.push(hm);
      
      iteration_i++;
      if (iteration_i >= plots)
      {
        iteration_i = 0;
        iteration_j++;
        if (iteration_j >= plots)
          return true;
      }
      
      if (_camController)
        _camController.target = new Vector3D(hm.width * plots / 2, 0, hm.width * plots / 2);
      
      return false;
    }
    
    public var heightMaps:Vector.<HeightMap> = new Vector.<HeightMap>();
    
  }

}